package com.fh.modules.dbms.controller;

import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.util.StringUtils;
import com.fh.modules.dbms.entity.DbmsPackage;
import com.fh.modules.dbms.entity.DbmsPackageBody;
import com.fh.modules.dbms.entity.DbmsPackageDeclare;
import com.fh.modules.dbms.model.CompareResultModel;
import com.fh.modules.dbms.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author qiujunda
 * @Date 29/07/2020
 * @Description 对比controller
 */
@RestController
@Api("数据库对比")
public class DbmsCompareController {
    @Autowired
    private IDbmsViewService dbmsViewService;
    @Autowired
    private IDbmsTableService dbmsTableService;
    @Autowired
    private IDbmsPackageService dbmsPackageService;
    @Autowired
    private IDbmsPackageDeclareService dbmsPackageDeclareService;
    @Autowired
    private IDbmsPackageBodyService dbmsPackageBodyService;

    /**
     * @param sourceId1 源数据源 必填
     * @param sourceId2 待对比数据源 必填
     * @param viewName 视图名 非必填
     * @param diff 0,1  0不同的，1相同的 非必填
     * @return
     */
    @GetMapping(value = "/dbms/compareDataSource/view")
    @ApiOperation("视图对比")
    @AutoLog(value = "视图对比")
    public Result compareView(@RequestParam(name = "sourceId1") Long sourceId1,
                              @RequestParam(name = "sourceId2") Long sourceId2,
                              String viewName,
                              Integer diff){
        viewName= StringUtils.toUpperCase(viewName);
        return Result.ok(dbmsViewService.compareView(sourceId1, sourceId2, StringUtils.convertString(viewName,true), diff));
    }

    /**
     * @param sourceId1 源数据源 必填
     * @param sourceId2 待对比数据源 必填
     * @param tableName 表名 非必填
     * @param diff 0,1  0不同的，1相同的 非必填
     * @return
     */
    @GetMapping(value = "/dbms/compareDataSource/table")
    @ApiOperation("表对比")
    @AutoLog(value = "表对比")
    public Result compareTable(@RequestParam(name = "sourceId1") Long sourceId1,
                               @RequestParam(name = "sourceId2") Long sourceId2,
                               String tableName,
                               Integer diff){
        tableName= StringUtils.toUpperCase(tableName);
        return Result.ok(dbmsTableService.compareTable(sourceId1,sourceId2,tableName,diff));
    }

    /**
     * @param sourceId1 源数据源 必填
     * @param sourceId2 待对比数据源 必填
     * @param packageName 包名 非必填
     * @param diff 0,1  0不同的，1相同的 非必填
     * @return
     */
    @GetMapping(value = "/dbms/compareDataSource/package")
    @ApiOperation("包对比")
    @AutoLog(value = "包对比")
    public Result comparePackage(@RequestParam(name = "sourceId1") Long sourceId1,
                                 @RequestParam(name = "sourceId2") Long sourceId2,
                                 String packageName,
                                 Integer diff){
        packageName= StringUtils.toUpperCase(packageName);
        return Result.ok(dbmsPackageService.comparePackage(sourceId1,sourceId2,packageName,diff));
    }

    @GetMapping(value = "/dbms/queryContent/queryPackageDeclareContent")
    @ApiOperation("查询对比包声明内容")
    @AutoLog(value = "查询对比包声明内容" , logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPackageDeclareContent(@RequestParam(name = "sourceId1") Long sourceId1,
                                             @RequestParam(name = "sourceId2") Long sourceId2,
                                             @RequestParam(name = "packageName") String packageName){
        packageName= StringUtils.toUpperCase(packageName);
        DbmsPackage dbmsPackage1=dbmsPackageService.queryDbmsPackage(sourceId1,packageName);
        StringBuilder stringBuilder1=new StringBuilder(10240);
        if(dbmsPackage1!=null){
            List<DbmsPackageDeclare> list=dbmsPackageDeclareService.queryDbmsPackageDeclares(dbmsPackage1.getId());
            list.forEach(dbmsPackageDeclare -> {
                stringBuilder1.append(dbmsPackageDeclare.getContent());
            });
        }
        //防止内存溢出，手动置null
        dbmsPackage1=null;
        StringBuilder stringBuilder2=new StringBuilder(10240);
        DbmsPackage dbmsPackage2=dbmsPackageService.queryDbmsPackage(sourceId2,packageName);
        if(dbmsPackage2!=null){
            List<DbmsPackageDeclare> list=dbmsPackageDeclareService.queryDbmsPackageDeclares(dbmsPackage2.getId());
            list.forEach(dbmsPackageDeclare -> {
                stringBuilder2.append(dbmsPackageDeclare.getContent());
            });
        }
        return Result.ok(new CompareResultModel(stringBuilder1.toString(),stringBuilder2.toString()));
    }

    @GetMapping(value = "/dbms/queryContent/queryPackageBodyContent")
    @ApiOperation("查询对比包体内容")
    @AutoLog(value = "查询对比包体内容" , logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPackageBodyContent(@RequestParam(name = "sourceId1") Long sourceId1,
                                             @RequestParam(name = "sourceId2") Long sourceId2,
                                             @RequestParam(name = "packageName") String packageName){
        packageName= StringUtils.toUpperCase(packageName);
        DbmsPackage dbmsPackage1=dbmsPackageService.queryDbmsPackage(sourceId1,packageName);
        StringBuilder stringBuilder1=new StringBuilder(10240);
        if(dbmsPackage1!=null){
            List<DbmsPackageBody> list=dbmsPackageBodyService.queryDbmsPackageBodys(dbmsPackage1.getId());
            list.forEach(dbmsPackageDeclare -> {
                stringBuilder1.append(dbmsPackageDeclare.getContent());
            });
        }
        //防止内存溢出，手动置null
        dbmsPackage1=null;
        StringBuilder stringBuilder2=new StringBuilder(10240);
        DbmsPackage dbmsPackage2=dbmsPackageService.queryDbmsPackage(sourceId2,packageName);
        if(dbmsPackage2!=null){
            List<DbmsPackageBody> list=dbmsPackageBodyService.queryDbmsPackageBodys(dbmsPackage2.getId());
            list.forEach(dbmsPackageDeclare -> {
                stringBuilder2.append(dbmsPackageDeclare.getContent());
            });
        }
        return Result.ok(new CompareResultModel(stringBuilder1.toString(),stringBuilder2.toString()));
    }

    @GetMapping(value = "/dbms/queryContent/queryTableColumnContent")
    @ApiOperation("查询对比表字段内容")
    @AutoLog(value = "查询对比表字段内容" , logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryTableColumnContent(@RequestParam(name = "sourceId1") Long sourceId1,
                                          @RequestParam(name = "sourceId2") Long sourceId2,
                                          @RequestParam(name = "tableName") String tableName){
        tableName= StringUtils.toUpperCase(tableName);
        return Result.ok(dbmsTableService.queryTableColumnContent(dbmsTableService.queryDbmsTable(sourceId1,tableName),
                dbmsTableService.queryDbmsTable(sourceId2,tableName)));
    }

    @GetMapping(value = "/dbms/queryContent/queryTableIndexContent")
    @ApiOperation("查询对比表索引内容")
    @AutoLog(value = "查询对比表索引内容" , logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryTableIndexContent(@RequestParam(name = "sourceId1") Long sourceId1,
                                         @RequestParam(name = "sourceId2") Long sourceId2,
                                         @RequestParam(name = "tableName") String tableName){
        tableName= StringUtils.toUpperCase(tableName);
        return Result.ok(dbmsTableService.queryTableIndexContent(dbmsTableService.queryDbmsTable(sourceId1,tableName),
                dbmsTableService.queryDbmsTable(sourceId2,tableName)));
    }
}
